
package cn.antia.cep.report.eastwood;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import cn.antia.util.*;
/**
 * @author Xiao Bin
 * @date 2008-02-29
 */
public class EastWoodUrlBuilder {
	/**
	 * 多条线的x,y数据点数据 (每条线的数据用 TreeMap存储)
	 * @param dataList 数据列表
	 * @return url串
	 */
	public static String chtLxyString(List<TreeMap<Integer,Integer>> dataList){
		StringBuilder sbData = new StringBuilder("cht=lxy&chd=t:");
		StringBuilder xstring = new StringBuilder();
		StringBuilder ystring = new StringBuilder();
		
		int yaxisMax = Maths.getMaxInteger(getMaxYData(dataList));
		int xaxisMax = Maths.getMaxInteger(getMaxXSize(dataList));
		
		boolean outNotFirst = false;
		for (TreeMap<Integer, Integer> data : dataList) {
			Iterator<Integer> keys = data.keySet().iterator();
			Integer key;
			boolean notFirst = false;
			while (keys.hasNext()) {
				key = (Integer) keys.next();
				if(notFirst){
					xstring.append(",");
					ystring.append(",");
				}else{
					notFirst = true;
				}
				//转换为适合 EastWood展示的数据,100以内的浮点数
				float xf = Maths.precision(key.intValue() * (float)(100.0 / xaxisMax),1);
				xstring.append(xf);
				int ydata = data.get(key);
				float yf = Maths.precision(ydata * (float)(100.0 / yaxisMax),1);
				ystring.append(yf);
			}
			if(outNotFirst){
				sbData.append("|");
			}else{
				outNotFirst = true;
			}
			sbData.append(xstring).append("|").append(ystring);
			xstring = new StringBuilder();
			ystring = new StringBuilder();
		}
		return sbData.toString();
	}
	/**
	 * 大小
	 * @param width
	 * @param heigth
	 * @return
	 */
	public static String chsString(int width,int heigth){
		return new StringBuilder("chs=").append(width).append("x").append(heigth).toString();
	}
	/**
	 * 将所有参数连接
	 * @param parameters 
	 * @return
	 */
	public static String jointUrlParameters(List<String> parameters){
		StringBuilder builder = new StringBuilder();
		boolean notFirst = false;
		for (String string : parameters) {
			if(notFirst){
				builder.append("&");
			}else{
				notFirst = true;
			}
			builder.append(string);
		}
		return builder.toString();
	}
	/**
	 * 颜色
	 * @param colors
	 * @return
	 */
	public static String chcoString(List<String> colors){
		StringBuilder builder = new StringBuilder("chco=");
		boolean notFirst = false;
		for (String color : colors) {
			if(notFirst){
				builder.append(",");
			}else{
				notFirst = true;
			}
			builder.append(color);
		}
		return builder.toString();
	}

	/**
	 * x,y 轴数据
	 * y轴设置为10个点
	 * x轴设置为10个点，如果不足10个点则设置为实际的数据点
	 * @param data	数据
	 * @param defaultAxisDesc	默认的横轴与竖轴描述
	 * @return
	 */
	public static String xyAxisString(TreeMap<Integer,Integer> data,Map<String,String> defaultAxisDesc){
		StringBuilder ysb = new StringBuilder("|0");
		StringBuilder xsb = new StringBuilder();
		boolean defaultX = defaultAxisDesc.get("X") != null;
		if(defaultX){
			xsb.append(defaultAxisDesc.get("X"));
		}else{
			//x 轴数据
			int xCount = data.size();
			if(xCount >= 10){
				xCount = Maths.getMaxInteger(xCount);
				int step = xCount / 10;
				for(int i = 1;i<=10;i++){
					xsb.append("|").append(step * i);
				}
			}else{
				for(int i = 1;i<=xCount;i++){
					xsb.append("|").append(i);
				}
			}
		}
		boolean defaultY = defaultAxisDesc.get("Y") != null;
		if(defaultY){
			ysb.append(defaultAxisDesc.get("Y"));
		}else{
			int ymax = getMaxYData(data);
			//为y最大值取整,一位数取2位的整数，二位数取三位的整数
			ymax = Maths.getMaxInteger(ymax);
			int step = ymax / 10;
			for(int i = 1;i<=10;i++){
				ysb.append("|").append(step * i);
			}
		}
		StringBuilder builder = new StringBuilder("chxt=x,y&chxl=");
		StringBuilder xs = new StringBuilder("0:").append(xsb.toString());
		StringBuilder ys = new StringBuilder("1:").append(ysb.toString());
		builder.append(xs).append("|").append(ys);
		return builder.toString();
	}
	//最大y数据
	private static int getMaxYData(List<TreeMap<Integer,Integer>> dataList){
		int max = 0;
		TreeMap<Integer,Integer> temp;
		int it = 0;
		for (int i = 0; i < dataList.size(); i++) {
			temp = dataList.get(i);
			it = getMaxYData(temp);
			if(it > max){
				max = it;
			}
		}
		return max;
	}
	//最大的数据点数
	private static int getMaxXSize(List<TreeMap<Integer,Integer>> dataList){
		int max = 0;
		int it = 0;
		for (int i = 0; i < dataList.size(); i++) {
			it = dataList.get(i).size();
			if(it > max){
				max = it;
			}
		}
		return max;
	}
	//最大y数据
	private static int getMaxYData(TreeMap<Integer,Integer> map){
		int max = 0;
		Iterator<Integer> iterator = map.values().iterator();
		while (iterator.hasNext()) {
			int elem = iterator.next();
			if(elem > max){
				max = elem;
			}
		}
		return max;
	}
}
